package com.netty.server;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.qbb.server.constanct.QbbConfigUtil;
import com.qbb.server.exception.NettyException;

/**
 * 类说明
 * 
 * @author chenhuaiao
 * @version V1.0 创建时间：Mar 18, 2015 10:55:08 AM
 */
public class QbbSocketServer extends HttpServlet {
	private static Log log = LogFactory.getLog(QbbSocketServer.class);
	public static WebApplicationContext application = null;

	public void init() throws ServletException {
		log.info("QbbSocketServer init begin ");
		// 获取WebApplicationContext
		ServletContext content = getServletContext();
		application = WebApplicationContextUtils
				.getWebApplicationContext(content);
		if (application == null) {
			throw new NullPointerException();
		}
		// 启动服务端的端口
		List<Integer> protLists = QbbConfigUtil.getPorts();
		if (protLists.isEmpty()) {
			throw new NettyException(
					"QbbServer init ports have exceptin: protLists is empty");
		}
		int protSize = protLists.size();
		for (int i = 0; i < protSize; i++) {
			int port = protLists.get(i);
			QbbServerThread server = new QbbServerThread(port);
			Thread thread = new Thread(server);
			thread.start();
		}
		log.info("QbbSocketServer init end");
	}

}
